﻿using PasswordManagerMVC.Models;
using PasswordManagerMVC.Utilities;
using PwdMgr.CommonContract;
using PwdMgr.PwdMgrService.DataService;
using PwdMgr.Utility.Crypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PasswordManagerMVC.Controllers
{
    public class SearchController : Controller
    {
        private IDataService _dataService;

        public SearchController()
        {
            _dataService = DataServiceFactory.CreateInstance();
        }
        //
        // GET: /Search/

        public ActionResult Index()
        {
            return RedirectToAction("Search");
        }

        public ActionResult AddNew()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddNew(ItemModel model)
        {
            if (ModelState.IsValid)
            {
                model.Password = CryptoUtil.Encrypt(model.Password, model.Key);
                SensitiveInfo info = Converter.ToSensitiveInfo(model);
                LogOnModel logOnModel;
                if (SessionManager.TryGetUser(HttpContext, out logOnModel))
                {
                    info.UID = logOnModel.ID;
                    try
                    {
                        _dataService.CreateSensitiveInfo(info);
                        return RedirectToAction("Search");
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("",
                            ex.ToString());
                    }
                }
            }
            return View(model);
        }

        public ActionResult Delete(int id)
        {
            _dataService.DeleteSensitiveInfo(id);

            string value = SessionManager.Get(HttpContext,
                "LatestSearchText") as string;

            return RedirectToAction("Search",
                new { SearchText = value });
        }

        public ActionResult Search(string SearchText)
        {
            if (string.IsNullOrEmpty(SearchText))
            {
                return View(new SearchModel 
                {
                    SearchText = SearchText,
                    Results = new List<ItemModel>(),
                });
            }

            try
            {
                LogOnModel model;
                if (SessionManager.TryGetUser(HttpContext, out model))
                {
                    var results = 
                        _dataService.GetSensitiveInfo(SearchText, model.ID);

                    SearchModel searchModel = new SearchModel();
                    searchModel.SearchText = SearchText;
                    searchModel.Results = results.ConvertAll(
                        s => Converter.ToItemModel(s));

                    SessionManager.Add(HttpContext, "LatestSearchText",
                        SearchText);
                    return View(searchModel);
                }

                return RedirectToAction("LogOn", "Account");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("",
                    ex.ToString());
            }
            return View();
        }
    }
}
